import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Bulletin } from "../../entities/bulletin.entities";
import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';

@Injectable()
export class BulletinService extends TypeOrmCrudService<Bulletin>{
  constructor(
    @InjectRepository(Bulletin)
    private readonly bulletinRepository:Repository<Bulletin>
  ){
    super(bulletinRepository);
  }
  async getBulletinList(order:any={ sort: "DESC", add_time: "DESC" },page:number=1,limit:number=10){
    let bulletins = await this.bulletinRepository.find({
      where: {
        enabled: 1
      },
      order:order,
      skip: (page -1)*limit,
      take: limit,
    });
    let total:number = await this.bulletinRepository.count({
      where: {
        enabled: 1
      }
    });
    return  {
      bulletins: bulletins,
      page: page++,
      limit:limit,
      total:Math.ceil(total/limit)
    };
  }
}
